package org.oncase.smcase.action;

import java.util.HashMap;
import java.util.Map;

import org.oncase.smcase.controller.PerspectiveController;
import org.oncase.smcase.entity.Perspective;

import com.google.inject.Inject;
import com.opensymphony.xwork2.ActionSupport;

public class PerspectiveAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private String name;
	
	private String description;
	
	private String initials;
	
	private String action;
	
	private int perspID;
	
	private Map<String, Object> ajaxResponse = new HashMap<String, Object>();
	
	private PerspectiveController perspectiveCon;
	
	@Inject
	public PerspectiveAction(PerspectiveController perspectiveCon) {
		this.perspectiveCon = perspectiveCon;
	}
	
	@Override
	public String execute() throws Exception {
		String returnView = ERROR;
		
		if (this.getName() != null && !this.getName().trim().equals("")) {
			if (this.getDescription() != null && !this.getDescription().trim().equals("")) {
				
				Perspective perspective = null;
				
				if (this.action != null && this.action.equals("insert")) {
					perspective = new Perspective(this.name.trim(), this.description.trim());
					
				} else if (this.action != null && this.action.equals("update") && this.perspID > 0) {
					perspective = this.perspectiveCon.findByPK(this.perspID);
					perspective.setName(name);
					perspective.setDescription(this.description.trim());
				}
				
				if (perspective != null) {
					
					if (this.getInitials() != null && !this.getInitials().trim().equals("")) {
						perspective.setInitials(this.getInitials().trim());
					}
					
					if (this.action.equals("insert")) {
						this.perspectiveCon.insert(perspective);
					} else {
						this.perspectiveCon.update(perspective);
					}
					
					// Returned for add the relative objectives
					this.ajaxResponse.put("perspID", perspective.getId());

					returnView = SUCCESS;
				} else {
					this.ajaxResponse.put(ERROR, "Undefined error, perspective not found.");
				}
			} else {
				this.ajaxResponse.put(ERROR, "The field description is required.");
			}
		} else {
			this.ajaxResponse.put(ERROR, "The field name is required.");
		}
		
		return returnView;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public int getPerspID() {
		return perspID;
	}

	public void setPerspID(int perspID) {
		this.perspID = perspID;
	}

	public Map<String, Object> getAjaxResponse() {
		return ajaxResponse;
	}

	public void setAjaxResponse(Map<String, Object> ajaxResponse) {
		this.ajaxResponse = ajaxResponse;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getInitials() {
		return initials;
	}

	public void setInitials(String initials) {
		this.initials = initials;
	}

	
}
